#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>


int main(int argc, char *argv[])
{
    int fd;    
    char buf1[32];
    char buf2[32] = "nihao";
    
    if (argc < 2)
    {
        printf("%d\r\n", argc);
        printf("plase input paraments\r\n");
        return -1;
    }

    fd = open(argv[1], O_RDWR, 0666);
    if (fd < 0)
    {
        
        printf(" open %s is error: %s\r\n", argv[1], strerror(errno));
        return -1;
    }
    printf("open %s is ok\r\n", argv[1]);

    if (argc > 2)
    {
        if (! strcmp(argv[2], "read"))
        {
            read(fd, buf1, 32);
            printf("read : %s\n", buf1);

        } else if (! strcmp(argv[2], "write"))
        {
            write(fd, buf2, 6);
        }
    }

    close(fd);

    return 0;
}
